package com.example.demo;
import com.alibaba.fastjson.JSON;
import com.example.demo.controller.WebSocketController;
import com.example.demo.repository.AutoMessageHandler;
import com.example.demo.service.SpyderCOVID;
import com.example.demo.service.SpyderInterfaceLight;
import com.normalClasses.eye.*;
import java.io.IOException;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SocketMessageMaker {
	@Autowired
	private AutoMessageHandler messageHandler;
	@Autowired
	private SpyderInterfaceLight spyder;
	@Autowired
	private SpyderCOVID feiyan;


	@Scheduled(fixedDelay=1700)
	public void mainhandler(){
		//一切基于当有活跃用户在线时，消息制造器开始制造
		//当没有新的用户连接时，将会一直阻塞
		String userid="";
		try {
		userid= WebSocketController.schedule_message_handler.pop();
		}
		catch(Exception e){
			log.error(LogUtil.logSpec(LogUtil.logSpec("schedule_message_handler maybe has pop problem, or system forced shutdown")));
		}
		//再次检查一边是否制造过消息
		if(messageHandler.Auto_check_scheduleTask(userid)){	
		messageHandler.Auto_deposit_message(userid,messageHandler.Auto_time_warning(userid),"true",1,0);	
		messageHandler.Auto_deposit_message(userid,messageHandler.Auto_weather_warning(userid),"true",2,0);
		String[] msg=messageHandler.Auto_operation_notice(userid);
		if(msg!=null){
        for(int i=3;i<6;i++){
        	messageHandler.Auto_deposit_message(userid,msg[i-3],"true",i,0);	
        }
		}
		messageHandler.Auto_deposit_message(userid,((Lambda_fun)(cookie,msg1)->{ 
			   String username=messageHandler.Auto_getusername(cookie);
			   msg1.setTitle("您的回馈");
			   msg1.setContent(spyder.get_user_notification(username));
			   return JSON.toJSONString(msg1);
			   }).getUserReportFeedback(userid,new message()),"true",6,0);
		messageHandler.Auto_deposit_message(userid,messageHandler.Auto_feiyan_warning(userid),"true",7,0);
		//后续可以添加更多的优先级最低的定时通知任务
		//触动更改一次执行权限
		messageHandler.Auto_sign_maker(userid);
	}
//System.out.println("消息制造器执行了一次");		
//System.out.println("消息制造器定时检查了一次");
}


@Scheduled(fixedDelay=1810000)
public void residual_message(){
	//检查当有用户不在线时的主要处理
	String username[]=messageHandler.Auto_find_username();
	for(int i=0;i<username.length;i++){
		//检查此时此人是否在线，如果在线，则不处理，没有则处理	
        if(!this.check_isonline(username[i]))
		messageHandler.Auto_send_mail(username[i]);
	 }
	log.info(LogUtil.logSpec(LogUtil.logSpec("residual_message carry out once")));
}


public boolean check_isonline(String username) {
    String onlineuserid[]= WebSocketController.getonlineUserid();
    	if(onlineuserid.length>0){
           	for(int j=0;j<onlineuserid.length;j++) {
           		
           		if(username.equals(messageHandler.Auto_getusername(onlineuserid[j]))) {
           		  return true;	
           		}
           	}
           }
    return false;
       
     }
    //尽可能设置的没有公倍数 
    @Scheduled(fixedDelay=20801111) 
    public void feiyan_mysql(){
    	try {
			feiyan.get_feiyan_index();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error(LogUtil.logSpec(LogUtil.logSpec("There are some problems with the feiyan, which may be the outdated interface" +
					" or network problems")));
		}
    	
    	
//    	//当没有一个人在线时执行的清理数据库行为和处理cookie过期情况
//    	//但无法响应退出异常情况，因为退出异常会使系统一直认为有用户在线
//    	if(!messageHandler.Auto_check_onlinepeople()){
//    	//System.out.println("执行了检查！");
//    	messageHandler.Auto_clean_mysql();
//    	messageHandler.Auto_handle_timeout_cookie();
//    	}	
//    //修改异常登陆情况，使用socket的静态方法
//    	else if(websocket.getOnlineNum()==0) {
//    		messageHandler.Auto_handle_login_exception();
//    	}
    	
    }	
}
